home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11179 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  56 lines

  1. Path: news.Arizona.EDU!seds!chrisl
  2. From: chrisl@SEDS.LPL.Arizona.EDU (Chris Lewicki)
  3. Newsgroups: comp.lang.c
  4. Subject: 12 bit datatype in binary file - misaligned
  5. Date: 22 Mar 1996 09:13:13 GMT
  6. Organization: The University of Arizona
  7. Message-ID: <4itqv9$kcq@news.ccit.arizona.edu>
  8. NNTP-Posting-Host: seds.lpl.arizona.edu
  9.  
  10. Hello All,
  11.     I've been scouring the net and the bookstores for the last few
  12. days trying to figure out how to read a 12-bit data length out of a
  13. binary file.
  14.     Here's the setup:  I have a data file that has all lengths of
  15. fields in it.  There are 1, 2, 3, 4, 6, 8, 12, and 16 bit data types
  16. all packed in very efficiently.  I've figured out that you can define
  17. a structure:
  18.  
  19. struct BitData {
  20.     unsigned onebit : 1;    
  21.     unsigned anotheronebit : 1;
  22.     unsigned fourbit : 4;
  23.     unsigned twobit : 2;
  24.     unsigned : 4; /* 4 bits padding */
  25.     unsigned char eightbit;
  26.     unsigned twelvebit : 12;
  27.     unsigned sixteenbit;
  28. };
  29.  
  30. struct BitData bitdata;
  31.  
  32.     Using this I could read in a file formatted this way with a simple 
  33. fread(&bitdata, ...) but I find that I encounter problems when I get
  34. to the 12 bit data types.  It will read in the first 12 bit variable
  35. encountered, but everything after that is misaligned.  I've searched
  36. the world over, but I can't find a thing on this topic.        
  37.     I understand that this is implementation dependent, and I am
  38. using gcc 2.7.2 (and/or SUNWspro cc) on a Sparc 10 running Solaris
  39. 2.5.  I've tried the Sun cc compiled using the -misalign compile flag,
  40. but that doesn't work either.
  41.     Will I be forced to read in binary blocks and parse them out?
  42. Please no!!! I'd much rather do a single fread, that a few hundred bit
  43. shifiting operations!
  44.     
  45. I just checked this group for the first time in a while, and saw quite
  46. a few things on bit operations, but I didn't see anything in the FAQ
  47. relating to my problem.  Anyway, I don't hang out here too much, so
  48. please cc: me if you happen to know the fix!
  49.  
  50. Eternally indebted...
  51.  
  52. --
  53. ------------------------------------------------------------------------------
  54. Christopher A. Lewicki KC7NYV 520.703.5542 Maintainer of SEDS.LPL.Arizona.EDU
  55.    Chair, Students for the Exploration and Development of Space (SEDS-USA)
  56.